package com.donleo.oauth2.order.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liangd
 * @since 2021-03-10 19:32
 */
@RestController
@RequestMapping("/r")
public class OrderController {

    @GetMapping("/r1")
    @PreAuthorize("hasAnyAuthority('user:add')")
    public String test() {
        //从安全上下文中获取用户身份信息，用户身份信息在网关处添加到了安全上下文中
        String principal = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = (String) JSONObject.parseObject(principal).get("name");
        return name + "访问了------->订单资源服务";
    }
}
